package ccnt.pvc.scudwaremobile.storage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import ccnt.pvc.scudwaremobile.dao.ApkModel;
import ccnt.pvc.scudwaremobile.storage.ServiceContract.ApkEntry;

public class ApkDbManager {

	private Context context;
	private ServiceDbHelper serviceDbHelper = null;
	
	private final String[] apk_projection = {
		ApkEntry.COLUMN_ID,
		ApkEntry.COLUMN_APK_NAME,
		ApkEntry.COLUMN_PACKAGE_NAME,
		ApkEntry.COLUMN_VERSION,
		ApkEntry.COLUMN_DEVELOPER,
		ApkEntry.COLUMN_IS_INSTALLED,
		ApkEntry.COLUMN_GMTCREATED,
		ApkEntry.COLUMN_GMTMODIFIED,
		ApkEntry.COLUMN_IS_DELETED
	};
	
	public ApkDbManager(Context context) {
		this.context = context;
	}
	
	public ServiceDbHelper getDbHelper() {
		if(serviceDbHelper == null) {
			serviceDbHelper = new ServiceDbHelper(context);
		}
		return serviceDbHelper;
	}
	
	/**
	 * 插入一条APK记录
	 * @param apkModel
	 * @return 记录id
	 */
	public long insertApk(ApkModel apkModel) {
		SQLiteDatabase db = getDbHelper().getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(ApkEntry.COLUMN_APK_NAME, apkModel.getApkName());
		values.put(ApkEntry.COLUMN_PACKAGE_NAME, apkModel.getPackageName());
		values.put(ApkEntry.COLUMN_VERSION, apkModel.getVersion());
		values.put(ApkEntry.COLUMN_DEVELOPER, apkModel.getDeveloper());
		values.put(ApkEntry.COLUMN_IS_INSTALLED, 0);
		values.put(ApkEntry.COLUMN_GMTCREATED, apkModel.getGmtCreated().toString());
		values.put(ApkEntry.COLUMN_GMTMODIFIED, apkModel.getGmtModified().toString());
		values.put(ApkEntry.COLUMN_IS_DELETED, 0);
		long rowId = db.insert(ApkEntry.TABLE_NAME, null, values);
		db.close();
		return rowId;
	}
	
	/**
	 * 插入多条APK记录
	 * @param apkModels apk列表
	 * @return 插入记录条数
	 */
	public int insertApks(List<ApkModel> apkModels) {
		SQLiteDatabase db = getDbHelper().getWritableDatabase();
		for(ApkModel apkModel : apkModels) {
			ContentValues values = new ContentValues();
			values.put(ApkEntry.COLUMN_APK_NAME, apkModel.getApkName());
			values.put(ApkEntry.COLUMN_PACKAGE_NAME, apkModel.getPackageName());
			values.put(ApkEntry.COLUMN_VERSION, apkModel.getVersion());
			values.put(ApkEntry.COLUMN_DEVELOPER, apkModel.getDeveloper());
			values.put(ApkEntry.COLUMN_IS_INSTALLED, 0);
			values.put(ApkEntry.COLUMN_GMTCREATED, apkModel.getGmtCreated().toString());
			values.put(ApkEntry.COLUMN_GMTMODIFIED, apkModel.getGmtModified().toString());
			values.put(ApkEntry.COLUMN_IS_DELETED, 0);
			db.insert(ApkEntry.TABLE_NAME, null, values);
		}
		db.close();
		return apkModels.size();
	}
	
	/**
	 * 获得所有的apk
	 * @return apk列表
	 */
	public List<ApkModel> getAllApks() {
		List<ApkModel> apkModels = new ArrayList<ApkModel>();
		SQLiteDatabase db = getDbHelper().getReadableDatabase();
		Cursor cursor = db.query(ApkEntry.TABLE_NAME, apk_projection, null,
				null, null, null, null);
		while(cursor.moveToNext()) {
			apkModels.add(fillApkModel(cursor));
		}
		db.close();
		return apkModels;
	}
	
	private ApkModel fillApkModel(Cursor cursor) {
		ApkModel apkModel = new ApkModel();
		apkModel.setId(cursor.getInt(cursor.getColumnIndex(ApkEntry.COLUMN_ID)));
		apkModel.setApkName(cursor.getString(cursor.getColumnIndex(ApkEntry.COLUMN_APK_NAME)));
		apkModel.setPackageName(cursor.getString(cursor.getColumnIndex(ApkEntry.COLUMN_PACKAGE_NAME)));
		apkModel.setVersion(cursor.getString(cursor.getColumnIndex(ApkEntry.COLUMN_VERSION)));
		apkModel.setDeveloper(cursor.getString(cursor.getColumnIndex(ApkEntry.COLUMN_DEVELOPER)));
		apkModel.setIsInstalled(cursor.getInt(cursor.getColumnIndex(ApkEntry.COLUMN_IS_INSTALLED)));
		return apkModel;
	}
	
	/**
	 * apk条目数
	 * @return 条目数
	 */
	public int countApks() {
		return getAllApks().size();
	}
	
	/**
	 * 修改apk安装信息
	 * @param packageName 包名
	 * @param isInstalled 是否安装，0表示未安装，1表示已安装
	 */
	public void setIsInstalled(String packageName, int isInstalled) {
		SQLiteDatabase db = getDbHelper().getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(ApkEntry.COLUMN_IS_INSTALLED, isInstalled);
		db.update(ApkEntry.TABLE_NAME, values, ApkEntry.COLUMN_PACKAGE_NAME
				+ " = ?", new String[] { packageName });
		db.close();
	}
	
	/**
	 * 根据包名查找apk
	 * @param packageName 包名
	 * @return apk信息
	 */
	public ApkModel getApkByPackageName(String packageName) {
		ApkModel apkModel = null;
		SQLiteDatabase db = getDbHelper().getReadableDatabase();
		Cursor cursor = db.query(ApkEntry.TABLE_NAME, apk_projection,
				ApkEntry.COLUMN_PACKAGE_NAME + " = ?",
				new String[] { packageName }, null, null, null);
		if(cursor.moveToFirst()) {
			apkModel = fillApkModel(cursor);
		}
		db.close();
		return apkModel;
	}
	
}
